<?php
// +----------------------------------------------------------------------
// | 分销联盟模块类
// +----------------------------------------------------------------------
// | Author: TongBC <50249101@qq.com>
// +----------------------------------------------------------------------
// | Date  : 2015/03/06
// +----------------------------------------------------------------------

class Alliance{

   
    // 账号转转化成联盟
    public function transform(Array $data){
        if($this->chExists($data['fid'])) return array('code'=>101,'msg'=>'该会员已转换为联盟账号');
		$sql="insert into `pft_distributor_union_SE` set fid='{$data['fid']}',dtitle='{$data['dtitle']}',dratio='{$data['dratio']}',dratio_default='{$data['dratio_default']}',protocol='{$data['protocol']}',dmemo='{$data['dmemo']}',createtime=now(),dstatus=0";
        $code = $GLOBALS['le']->query($sql) ? 100:101;
        return array('code'=>$code);
    }

	
    // 获取联盟信息
    public static function info($mid){
        $sql="select * from `pft_distributor_union_SE` where fid=$mid limit 1";
        $GLOBALS['le']->query($sql);
        return $GLOBALS['le']->fetch_assoc();
    }
    
    // 根据会员 ID 检测联盟是否存在
    public function chExists($mid){
        $sql="select * from `pft_distributor_union_SE` where fid=$mid limit 1";
        $GLOBALS['le']->query($sql);
        $GLOBALS['le']->fetch_assoc();
        return ($GLOBALS['le']->f('id')) ? true:false;
    }
    
    // 检测联盟名称是否存在
    public function chExistsName($name,$uid=0){
        if($uid) $and = "and id <> $uid";
        $sql="select id from `pft_distributor_union_SE` WHERE dtitle='$name' $and limit 1";
		$GLOBALS['le']->query($sql);
		$GLOBALS['le']->fetch_assoc();
        return ($GLOBALS['le']->f('id')) ? true:false;
    }
    
    // 更新联盟资料
    public static function modify(Array $data,$fid=0){
		if($fid==0) $fid = $_SESSION['sid'];
        $sql="update `pft_distributor_union_SE` set `dtitle`='{$data['dtitle']}',`dratio`='{$data['dratio']}',`dmemo`='{$data['dmemo']}',protocol='{$data['protocol']}' where id={$data['id']} and fid=$fid limit 1";
		return $GLOBALS['le']->query($sql);
    }
	



	// 转分销产品开启转供应时配置给联盟成员
	public static function setEvoluteList($pid, $apply_did, $memberID=0){
		
		if($memberID==0) $memberID = $_SESSION['sid'];// 盟主ID
		
		// 1.获取分销商
		$distributors = array();
		$sql = "select r.id,r.son_id,m.dname from pft_member_relationship r left join pft_member m on r.son_id=m.id where r.parent_id=$memberID and r.ship_type=0 and r.status=0 and son_id<>$memberID and m.status<3 and m.id>1";
		$GLOBALS['le']->query($sql);
		while($row=$GLOBALS['le']->fetch_assoc()) $distributors[$row['son_id']] = $row['dname'];
		$dist_list_a = array_filter(array_keys($distributors));

		$sql = "select memberID from pft_union_member_info_SE where fid=$memberID and dstatus=0";
		$GLOBALS['le']->query($sql);
		while($row=$GLOBALS['le']->fetch_assoc()) $dist_list_b[] = $row['memberID'];
		$dist_list_a = array_intersect($dist_list_a, $dist_list_b);
		$dist_list_s = implode(',', $dist_list_a);
		$dist_list_l = count($dist_list_a);

		// 2.获取已设置的记录
		$evolute = array();
		$sql = "select id,aids,lvl,status,fid from `pft_p_apply_evolute` where sid=$memberID and pid=$pid and fid in ($dist_list_s) limit $dist_list_l";
		$GLOBALS['le']->query($sql);
		while($row=$GLOBALS['le']->fetch_assoc()) $evolute[$row['fid']] = $row;

		// 3.获取供应商给我的信息
		$sql="select id,aids,lvl,sourceid from pft_p_apply_evolute where sid='$apply_did' and fid=$memberID and pid=$pid and active=1 limit 1";
		$GLOBALS['le']->query($sql);
		$dbData  = $GLOBALS['le']->fetch_assoc();
		$chain_a = explode(',', $dbData['aids']);
		$chain_n = $dbData['aids'].','.$memberID;
		$evo_lvl = count($chain_a) + 1;

		// 4.开始设置分销商购买权限
		$ins   = "insert into pft_p_apply_evolute (sid,fid,sourceid,pid,lvl,status,aids,rectime) values ";
		$ins_f = false;
		foreach($dist_list_a as $dist_mid){
			if(in_array($dist_mid, $chain_a)) continue;// 去掉供应链上的分销商
			
			if(isset($evolute[$dist_mid])){// 存在之前设置过的记录
				$evolute_t = $evolute[$dist_mid];  // 过滤正确的供应链
				if($evolute_t['status']==0 && $evolute_t['aids']==$chain_n && $evolute_t['lvl']==$evo_lvl) continue;
				$upd_ids_a[] = $evolute_t['id'];continue;
			}
			
			// 新增
			$ins_f = true;
			$ins .= "($memberID,$dist_mid,{$dbData['sourceid']},$pid,$evo_lvl, 0, '$chain_n', now()),";
		}

		if($ins_f) $GLOBALS['le']->query(rtrim($ins, ','));

		// 更新状态
		if(($upd_ids_l = count($upd_ids_a))){
			$upd_ids_s = implode(',', $upd_ids_a);
			$upd = "update pft_p_apply_evolute set status=0,aids='$chain_n',lvl=$evo_lvl where id in($upd_ids_s) and sid=$memberID limit $upd_ids_l";
			$GLOBALS['le']->query($upd);
		}
	}
      


}
